// pagesFarm/pages/poultrySign/index.js
var app = getApp(),
  fetchApi = require("../../../utils/httpPost.js"),
  formatTime = require("../../../component/common.js");
  var context = null;// 使用 wx.createContext 获取绘图上下文 context
var isButtonDown = false;//是否在绘制中
var arrx = [];//动作横坐标
var arry = [];//动作纵坐标
var arrz = [];//总做状态，标识按下到抬起的一个组合
var canvasw = 0;//画布宽度
var canvash = 0;//画布高度

Page({

  /**
   * 页面的初始数据
   */
  data: {
    adopt:{},
    adoptId:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.data.adoptId = parseInt(options.id);
      this.onHttpInit();
    }
    this.startCanvas();
  },
  /**
   *  获取领养中心列表
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/farm/poultry/adopt/detail', {
      adopt_id: that.data.adoptId,
    }, function (res) {
      console.log("获取领养详情", res)
      if (res.data.code == 0) {
        that.setData({
          hasData: true,
          adopt: res.data.data.adopt,
        })
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false })
    })
  },
  /**
  * 以下 - 手写签名 / 上传签名
  */
 startCanvas: function () {//画布初始化执行
  var that = this;
  //获取系统信息
  wx.getSystemInfo({
   success: function (res) {
    canvasw = res.windowWidth;
    canvash = res.windowHeight;
    that.setData({ canvasw: canvasw });
    that.setData({ canvash: canvash });
   }
  }); 
  this.initCanvas();
  this.cleardraw(); 
  },
  //初始化函数
  initCanvas: function () {
    context = wx.createCanvasContext('canvas');
    context.beginPath()
    context.fillStyle = 'rgba(255, 255, 255, 0)';
    context.setStrokeStyle('#000000');
    context.setLineWidth(4);
    context.setLineCap('round');
    context.setLineJoin('round');
  },
  canvasStart: function (event) {
    isButtonDown = true;
    arrz.push(0);
    arrx.push(event.changedTouches[0].x);
    arry.push(event.changedTouches[0].y);
  },
  canvasMove: function (event) {
    if (isButtonDown) {
      arrz.push(1);
      arrx.push(event.changedTouches[0].x);
      arry.push(event.changedTouches[0].y);
    }
    for (var i = 0; i < arrx.length; i++) {
      if (arrz[i] == 0) {
        context.moveTo(arrx[i], arry[i])
      } else {
        context.lineTo(arrx[i], arry[i])
      }
    }
    context.clearRect(0, 0, canvasw, canvash);
    context.setStrokeStyle('#000000');
    context.setLineWidth(4);
    context.setLineCap('round');
    context.setLineJoin('round');
    context.stroke();
    context.draw(false);
  },
  canvasEnd: function (event) {
    isButtonDown = false;
  },
  //清除画布
  cleardraw: function () {
    arrx = [];
    arry = [];
    arrz = [];
    context.clearRect(0, 0, canvasw, canvash);
    context.draw(true);
    this.data.signImg = "";
  },
  uploadSignImg(mer_id){
      var that = this
      //生成图片
      wx.canvasToTempFilePath({
        canvasId: 'canvas',
        //设置输出图片的宽高
        // destWidth:150, 
        // destHeight:150,
        // fileType:'jpg',
        quality:1.0,
        success: function (res) {
        console.log(res)
        // canvas图片地址 res.tempFilePath
        that.data.signImg = res.tempFilePath;

        let pages = getCurrentPages(); //获取当前页面pages里的所有信息。
        let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
        prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
          signImg: res.tempFilePath
        })
        wx.navigateBack({
          delta: -1,
        })
        },
        fail: function () {
          wx.showModal({
            title: '提示',
            content: 'canvas生成图片失败。微信当前版本不支持，请更新到最新版本！',
            showCancel: false
          });
        },
        complete: function () {}
      })
  }
})